<template>
  <div class="chat-send-area">
    <el-select
      v-if="categoryList?.length"
      v-model="queryForm.category"
      placeholder="请选择"
      size="default"
      style="width: 140px; position: absolute; right: 0px; top: -40px; z-index: 2"
    >
      <el-option v-for="item in categoryList" :key="item" :label="item" :value="item" />
    </el-select>
    <el-input
      v-model="messageVal"
      type="textarea"
      class="message-input"
      :disabled="loading || isRecording"
      :placeholder="'请输入您的问题，支持Shift+Enter换行'"
      :rows="3"
      @keydown="handleKeydown"
    />

    <VoiceButton
      class="voice-btn"
      ref="voiceButtonRef"
      v-model="messageVal"
      @start="isRecording = true"
      @end="isRecording = false"
    />
    <el-button
      class="send-btn"
      :type="!messageVal && !loading ? 'info' : 'primary'"
      :disabled="!messageVal && !loading"
      :loading="loading"
      @click="onSend"
    >
      <template #icon>
        <Icon icon="svg-icon:send" :size="20" />
      </template>
      <div class="pause-btn" v-if="loading" @click="onStop">
        <Icon icon="svg-icon:pause" :size="20" />
      </div>
    </el-button>
  </div>
</template>

<script setup>
import { ref, onUnmounted, inject } from 'vue'
import { ElMessage } from 'element-plus'
import VoiceButton from '@/components/Chat/VoiceButton.vue'

const props = defineProps({
  loading: {
    type: Boolean,
    default: false
  }
})

const categoryList = inject('useCategorySelect', [])
const queryForm = ref({})

const messageVal = ref('')
const isRecording = ref(false)
const voiceButtonRef = ref()

const emits = defineEmits(['send', 'stop'])

const onStop = () => {
  emits('stop')
}

const handleKeydown = (event) => {
  if (event.key === 'Enter' && !event.shiftKey) {
    event.preventDefault()
    onSend()
  }
}

const onSend = () => {
  let val = messageVal.value.trim()
  if (val !== '' && val !== null && val !== undefined) {
    // 如果正在录音，先停止录音
    if (isRecording.value) {
      voiceButtonRef.value?.doStop()
    }
    emits('send', messageVal.value, queryForm.value)
    messageVal.value = ''
  } else {
    ElMessage.error('请输入问题')
  }
}

const clearInputMessage = () => {
  messageVal.value = ''
}

defineExpose({
  clearInputMessage
})
</script>

<style lang="less" scoped>
.chat-send-area {
  position: relative;
  // padding-left: 40px;
  margin-top: 10px;
  .message-input {
    color: #434c59;
    border-radius: 6px;
    border: 1px solid #d3e4eb;
    background: #fff;
    box-shadow: 0px 1px 18px 1px rgba(203, 203, 203, 0.3);
    padding: 20px 18px;
    resize: none;
    &::placeholder {
      color: #434c59;
    }
  }

  :deep(.el-button) {
    width: 55px;
    height: 31px;
    &:hover {
      .pause-btn {
        display: flex;
      }
      .is-loading {
        display: none;
      }
    }
    &.is-loading {
      pointer-events: visible;
    }
    span {
      margin-left: 0;
    }
  }
  .send-btn {
    position: absolute;
    bottom: 30px;
    right: 30px;
  }
  .pause-btn {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    display: none;
    align-items: center;
    justify-content: center;
  }
  .voice-btn {
    position: absolute;
    bottom: 30px;
    right: 100px;
    width: 55px;
    height: 31px;
    font-size: 20px;
    line-height: 31px;
    padding: 2px;
    border: 0;
    // &.is-recording {
    // background-color: #ff4d4f;
    // }
    &:hover {
      .pause-btn {
        display: block;
      }
    }
  }

  .aubox {
    display: flex;
    justify-content: center;
    align-items: center; //重要属性，让hight翻转过来，往上延伸
  }
  .auItem {
    width: 2px;
    margin-right: 2px;
    margin-left: 2px;
    list-style: none;
    background: #fff;
    height: 20px;
  }
  .au1 {
    animation: audio 0.5s linear 0.1s infinite alternate;
  }
  .au2 {
    animation: audio 0.5s linear 0.2s infinite alternate;
  }
  .au3 {
    animation: audio 0.5s linear 0.5s infinite alternate;
  }
  // .au4 {
  // }
  .au5 {
    animation: audio 0.5s linear 0.2s infinite alternate;
  }
  .au6 {
    animation: audio 0.5s linear 0.3s infinite alternate;
  }
  .au7 {
    animation: audio 0.5s linear 0.1s infinite alternate;
  }
  @keyframes audio {
    from {
      height: 1px;
    }
    to {
      height: 20px;
    }
  }
}
</style>
